using System.Collections;
using System.Collections.Generic;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using Yoozoo.Core.Common;
using Yoozoo.Gameplay.Liberty;
using Yoozoo.Gameplay.Liberty.AI;
using Yoozoo.Gameplay.Liberty.StreetEvent;
using Yoozoo.Mars.Got;

namespace com.yoozoo.gta.Gameplay.Liberty.StreetEvent.BpNode
{
    public class AddStreetFightMonster : LatentActionNode<int, int>
    {
        protected List<AIBirthData> _aiBirthDataList;
        public int graphId = 1003;
        public int dropId = 0;
        public override IEnumerator Invoke(int grenerateNum, int initCounterValue)
        {
            var streetEvent = StreetFightEvent.Instance;
            _aiBirthDataList = new List<AIBirthData>();
            for (int i = 0; i < grenerateNum; i++)
            {
                var configList = streetEvent.eventConfig.npcConfigList;
                int randomNpcConfigIndex = UnityEngine.Random.Range(0,configList.Count);
                var npcId = configList[randomNpcConfigIndex].npcId;

                var bornPointList = streetEvent.BornPointList;
                int bornPointListIndex = UnityEngine.Random.Range(0,bornPointList.Length);
                var bornTrans = bornPointList[bornPointListIndex];
                AIBirthData birthData = new AIBirthData();
                birthData.uniqueId = NpcUniqueIdUtils.GetEventMonsterUniqueID();
                birthData.position = bornTrans.position;
                birthData.forward = bornTrans.forward;
                birthData.modelResId = npcId;
                birthData.animState = "";
                birthData.sideType = SideType.Protected;
                birthData.isBoss = false;
                birthData.isStreetEvent = true;
                birthData.isStatic = false;
                birthData.dropItemPackId = dropId;
                LibertyAIUtils.AIManager.AddNpc(birthData,graphId);
                _aiBirthDataList.Add(birthData);
                streetEvent.AddStreetId(i);
                StreetEventManager.GetInstance("StreetEventManager").OnAddMonster(birthData.uniqueId,i,string.Empty,birthData.sideType);
                // 给每个人加个积分器
                StreetEventManager.GetInstance("StreetEventManager").AddTaskCounter(birthData.uniqueId.ToString(), 0, TaskCounterMode.NoLimit, initCounterValue);
                // 给每个人加数据统计
                
                StreetEventManager.GetInstance("StreetEventManager").AddTaskCounter("Cash"+birthData.uniqueId, 0, TaskCounterMode.NoLimit, 0);
                // 击杀统计
                StreetEventManager.GetInstance("StreetEventManager").AddTaskCounter("Kill"+birthData.uniqueId, 0, TaskCounterMode.NoLimit, 0);
                // 死亡统计
                StreetEventManager.GetInstance("StreetEventManager").AddTaskCounter("Dead"+birthData.uniqueId, 0, TaskCounterMode.NoLimit, 0);
                yield return new WaitForOneFrame();
            }
        }
        
        public override void OnGraphStoped()
        {
            base.OnGraphStoped();
            if (_aiBirthDataList!=null)
            {
                foreach (var birthData in _aiBirthDataList)
                {
                    if (LibertyAIUtils.AIManager)
                    {
                        LibertyAIUtils.AIManager.RemoveNpc(birthData.uniqueId);
                    }
                }
            }
        }
    }
}